package pseudoAssemblerCompiler.twopassassembler.view.windows;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pseudoAssemblerCompiler.twopassassembler.controller.OperationCodeController;
import pseudoAssemblerCompiler.twopassassembler.exceptions.RequiredDataNotSetException;
import pseudoAssemblerCompiler.twopassassembler.model.windows.ProgramFileContentProvider;
import pseudoAssemblerCompiler.twopassassembler.model.windows.ProgramFileLabelProvider;
import pseudoAssemblerCompiler.twopassassembler.view.util.DialogWindowContainer;
import pseudoAssemblerCompiler.twopassassembler.view.util.LabeledContainer;
import pseudoAssemblerCompiler.twopassassembler.view.util.TableViewerContainer;

import java.io.File;
import java.net.URISyntaxException;

/**
 * User: Роман
 * Date: 22.03.12
 * Time: 18:18
 */
public class NewProgramFileWindow implements Window {

    private static final Logger LOGGER =
            LoggerFactory.getLogger(NewProgramFileWindow.class);

    public static final String NEW_PROGRAM_NAME = "Название программы: ";

    /**
     * Контроллер для вкладки редактора регламента опроса
     */
    private OperationCodeController _controller;

    private DialogWindowContainer _container;

    private LabeledContainer _labeledNewFile;

    private Text _newFile;

    private Button _open;

    private Button _cancel;

    public NewProgramFileWindow(Display aDisplay,
                                String aNameOfWindow) {

        _container = new DialogWindowContainer(aDisplay, aNameOfWindow, 600, 150);

        _labeledNewFile =
                new LabeledContainer(_container.get_composite(), NEW_PROGRAM_NAME);

        _newFile = new Text(_labeledNewFile.getControlComposite(), SWT.BORDER);

        _open = _container.createButton("Создать");
        _cancel = _container.createButton("Отменить");
    }

    private void layouts() {
        FormData formData;

        formData = new FormData();
        formData.left = new FormAttachment(0, 0);
        formData.top = new FormAttachment(0, 0);
        formData.right = new FormAttachment(100, 0);
        formData.height = 30;
        _labeledNewFile.setLayoutData(formData);

        formData = new FormData();
        formData.left = new FormAttachment(0, 0);
        formData.right = new FormAttachment(100, 0);
        _newFile.setLayoutData(formData);

        formData = new FormData();
        formData.top = new FormAttachment(0, 0);
        formData.right = new FormAttachment(_cancel, -10);
        formData.height = 30;
        formData.width = 100;
        _open.setLayoutData(formData);

        formData = new FormData();
        formData.top = new FormAttachment(0, 0);
        formData.right = new FormAttachment(100, -20);
        formData.height = 30;
        formData.width = 100;
        _cancel.setLayoutData(formData);
    }

    protected void listeners() {
        _cancel.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                _container.dispose();
            }
        });

        _open.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                try {
                    openButtonTriggered();
                    _container.get_shell().dispose();
                } catch (RequiredDataNotSetException ex) {
                    _container.makeMessage(ex.getMessage());
                    if (null != ex.getControl()) {
                        setFocus(ex.getControl());
                    }
                }
            }
        });
    }

    protected void openButtonTriggered() throws RequiredDataNotSetException {
        if (StringUtils.isEmpty(_newFile.getText())) {
            throw new RequiredDataNotSetException("Введите название файла или нажмите: Отмена.");
        }
        _controller.newProgram(_newFile.getText());
    }

    @Override
    public void create() {
        listeners();
        layouts();
        _container.open();
    }

    @Override
    public void setController(Object aController) {
        if (aController instanceof OperationCodeController) {
            _controller = (OperationCodeController) aController;
        }
    }

    public boolean setFocus(Control control) {
        return control.setFocus();
    }

}
